package com.bingxue.edu.teacher.service;


import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.management.education.model.entity.EnrollChangeRecord;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.EnrollChangeRecordService;
import com.bingxue.edu.management.education.service.EnrollmentService;
import com.bingxue.edu.teacher.model.query.TeacherClassChangeQuery;
import com.bingxue.edu.teacher.model.query.TeacherClassInfoQuery;
import com.bingxue.edu.teacher.model.query.TeacherClassStudentQuery;
import com.bingxue.edu.teacher.model.resp.TeacherClassInfoResp;
import com.bingxue.edu.teacher.model.resp.TeacherClassStudentResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.EnrollChangeRecordTableDef.ENROLL_CHANGE_RECORD;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;
import static com.bingxue.edu.management.education.model.entity.table.StudentTableDef.STUDENT;

@Slf4j
@Service
@RequiredArgsConstructor
public class TeacherClassInfoService {

    private final ClassInfoService classInfoService;
    private final EnrollmentService enrollmentService;
    private final EnrollChangeRecordService enrollChangeRecordService;

    public Pagination<TeacherClassInfoResp> getClassInfoPage(TeacherClassInfoQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        queryWrapper.orderBy(CLASS_INFO.ID.desc());
        Page<TeacherClassInfoResp> page = classInfoService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, TeacherClassInfoResp.class);
        return Pagination.of(page);
    }

    public List<TeacherClassStudentResp> getClassStudentList(TeacherClassStudentQuery query) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        ENROLLMENT.DEFAULT_COLUMNS,
                        STUDENT.DEFAULT_COLUMNS
                )
                .from(ENROLLMENT).leftJoin(STUDENT).on(ENROLLMENT.STUDENT_ID.eq(STUDENT.ID))
                .where(ENROLLMENT.CLASS_ID.eq(query.getClassId()))
                .and(ENROLLMENT.STATUS.eq(query.getStatus()));
        return enrollmentService.listAs(queryWrapper, TeacherClassStudentResp.class);
    }

    /**
     * 查询今日班级变更记录
     */
    public List<EnrollChangeRecord> getRecentChangeRecordList(TeacherClassChangeQuery query) {
        return enrollChangeRecordService.queryChain()
                .select(ENROLL_CHANGE_RECORD.ALL_COLUMNS)
                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLL_CHANGE_RECORD.ORIGINAL_CLASS_ID))
                .leftJoin(ENROLLMENT).on(ENROLLMENT.ID.eq(ENROLL_CHANGE_RECORD.ENROLLMENT_ID))
                .leftJoin(STUDENT).on(STUDENT.ID.eq(ENROLLMENT.STUDENT_ID))
                .where(CLASS_INFO.LEAD_TEACHER_ID.eq(query.getLeadTeacherId())).and(CLASS_INFO.ASSIST_TEACHER_ID.eq(query.getAssistTeacherId()))
                .and(ENROLL_CHANGE_RECORD.CREATE_TIME.ge(query.getStartDate()))
                .list();
    }

    public TeacherClassInfoResp getDetailById(String id) {
        return classInfoService.getMapper().selectOneWithRelationsByIdAs(id, TeacherClassInfoResp.class);
    }
}
